/**
 * @Title: UserController.java
 * @Package me.springboot.mybatis.web
 * @Description: TODO
 * @author zengh
 * @date 2018/04/23
 * @version V1.0
 */
package me.springboot.mybatis.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.springboot.mybatis.common.utils.*;
import me.springboot.mybatis.core.RetCode;
import me.springboot.mybatis.core.RetResponse;
import me.springboot.mybatis.core.RetResult;
import me.springboot.mybatis.exception.ResultException;
import me.springboot.mybatis.model.User;
import me.springboot.mybatis.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zengh
 * @ClassName: UserController
 * @Description: TODO
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    private static final Logger logger = Logger.getLogger(UserController.class);
    @Resource
    private UserService userService;

    /**
     * @param user
     * @Title: add
     * @Description: 添加客户
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add")
    public RetResult<Object> add(HttpServletRequest request, User user) throws ResultException {
        User currentUser = SessionUtil.getSessionUser(request);
        if (!currentUser.getType().equals("1")) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        user.setPassword(MD5Utils.md5("111111"));
        user.setType("3");
        userService.save(user);
        return RetResponse.makeOKRsp();
    }

    @PostMapping("/add/xunjian")
    public RetResult<Object> addXunJian(HttpServletRequest request, User user) throws ResultException {
        User currentUser = SessionUtil.getSessionUser(request);
        if (!currentUser.getType().equals("3")) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        user.setKehuId(currentUser.getuId());
        user.setPassword(MD5Utils.md5("123456"));
        user.setType("2");
        userService.save(user);
        return RetResponse.makeOKRsp();
    }

    @RequestMapping(value = "/loginOut", method = RequestMethod.POST)
    public RetResult<Integer> loginOut(HttpServletRequest request) throws ResultException {
        SessionUtil.removeUser(request);
        return RetResponse.makeOKRsp(1);
    }

    public static void main(String[] args) {
        RequestResult requestResult = HttpUtils.doGet("http://60.205.156.58:8080/smartcity/isok");
        Map<String, Object> resultMap  = JSONUtil.jsonToMap(requestResult.getResult());
        System.out.println(resultMap.get("state"));
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public RetResult<Map<String, Object>> login(HttpServletRequest request, @RequestParam String account,
                                                @RequestParam String password) throws ResultException {
        RequestResult requestResult = HttpUtils.doGet("http://60.205.156.58:8080/smartcity/isok");
        Map<String, Object> resultMap  = JSONUtil.jsonToMap(requestResult.getResult());
        if(!resultMap.get("state").toString().equals("1")) {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "登陆失败，程序出错，请联系管理员");
        }
        logger.info("account:" + account + ",password" + password);
        return RetResponse.makeOKRsp(userService.login(request, account, password));
    }

    @RequestMapping(value = "/phone/login", method = RequestMethod.POST)
    public RetResult<Map<String, Object>> phoneLogin(HttpServletRequest request, @RequestParam String account,
                                                     @RequestParam String password) throws ResultException {
        RequestResult requestResult = HttpUtils.doGet("http://60.205.156.58:8080/smartcity/isok");
        Map<String, Object> resultMap  = JSONUtil.jsonToMap(requestResult.getResult());
        if(!resultMap.get("state").toString().equals("1")) {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "登陆失败，程序出错，请联系管理员");
        }
        logger.info("account:" + account + ",password" + password);
        return RetResponse.makeOKRsp(userService.phoneLogin(request, account, password));
    }

    @RequestMapping(value = "/chargeMyPwd", method = RequestMethod.POST)
    public RetResult<Map<String, Object>> chargeMyPwd(HttpServletRequest request, @RequestParam String oldPwd,
                                                      @RequestParam String newPwd) throws ResultException {
        return RetResponse.makeOKRsp(userService.chargeMyPwd(request, oldPwd, newPwd));
    }

    @RequestMapping(value = "/chargePwd", method = RequestMethod.POST)
    public RetResult<Map<String, Object>> chargePwd(HttpServletRequest request, @RequestParam Integer uid,
                                                    @RequestParam String oldPwd, @RequestParam String newPwd) throws ResultException {
        return RetResponse.makeOKRsp(userService.chargePwd(request, uid, oldPwd, newPwd));
    }


    @RequestMapping(value = "/resertPwd", method = RequestMethod.POST)
    public RetResult<Map<String, Object>> resertPwd(HttpServletRequest request, @RequestParam String uid) throws ResultException {
        return RetResponse.makeOKRsp(userService.resertPwd(uid));
    }

    /**
     * @param user
     * @Title: add2
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add2")
    public RetResult<Object> add2(@RequestBody User user) {
        userService.save(user);
        return RetResponse.makeOKRsp();
    }

    /**
     * @Title: delete
     * @Description: 删除
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/delete")
    public RetResult<Object> delete(@RequestParam Integer id) {
        userService.deleteById(id);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param user
     * @Title: update
     * @Description: 更新
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update")
    public RetResult<Object> update(User user) {
        userService.update(user);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param user
     * @Title: update
     * @Description: 更新(传入参数为JSON格式)
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update2")
    public RetResult<Object> update2(@RequestBody User user) {
        userService.update(user);
        return RetResponse.makeOKRsp();
    }

    /**
     * @Title: detail
     * @Description:
     * @Reutrn RetResult<User>
     */
    @GetMapping("/detail")
    public RetResult<User> detail(HttpServletRequest request) throws ResultException {
        User user = SessionUtil.getSessionUser(request);
        return RetResponse.makeOKRsp(user);
    }

    /**
     * @param reqMap
     * @Title: detail
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<User>
     */
    @PostMapping("/detail2")
    public RetResult<User> detail(@RequestBody Map<String, Object> reqMap) {
        Integer id = Integer.valueOf(reqMap.get("id").toString());
        User user = userService.findById(id);
        return RetResponse.makeOKRsp(user);
    }

    /**
     * @param page 页码
     * @param size 每页条数
     * @Title: list
     * @Description: 客戶列表
     * @Reutrn RetResult<PageInfo<User>>
     */
    @PostMapping("/list")
    public RetResult<PageInfo<User>> list(HttpServletRequest request,
                                          @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "20") Integer size) throws ResultException {
        PageHelper.startPage(page, size);
        if(!RoleType.isSystemUser(request)) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        List<User> list = userService.findListBy("type", "3");
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

    @PostMapping("/xunjianList")
    public RetResult<PageInfo<User>> xunjianList(HttpServletRequest request, @RequestParam(defaultValue = "0") Integer page,
                                                 @RequestParam(defaultValue = "20") Integer size,
                                                 @RequestParam(required = false) String likeStr) throws ResultException {
        if(!RoleType.isKehu(request)) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        PageHelper.startPage(page, size);
        Map<String, Object> param = new HashMap();
        param.put("type", "2");
        param.put("kehu_id", SessionUtil.getSessionUser(request).getuId());
        param.put("likeStr", likeStr);
        List<User> list = userService.selectByMap(param);
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

    @GetMapping("/getAllKeHu")
    public RetResult<List<User>> getAllKeHu(HttpServletRequest request) throws ResultException {
        if(!RoleType.isSystemUser(request)) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        Map<String, Object> param = new HashMap();
        param.put("type", "3");
        List<User> list = userService.selectByMap(param);
        return RetResponse.makeOKRsp(list);
    }
}